﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_Entities;
using CoolERP_BLL;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class CGD_Edit : BasePage
    {
        private Guid CGDID
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            BindDepartment(this.ddlDepartments, "请选择");
            if (CGDID != Guid.Empty)
            {
                using (CGDBLL cgdB = new CGDBLL())
                {
                    CGD entity = cgdB.GetModel(t => t.ID == CGDID);
                    this.txtRQ.Text = entity.RQ == null ? "" : entity.RQ.ToString("yyyy-MM-dd HH:mm");
                }
            }

        }

        private void BindData()
        {
            if (CGDID != Guid.Empty)
            {
                liTitle.Text = "修改";
                using (CGDBLL cgdB = new CGDBLL())
                {
                    Dictionary<string, string> dc = new Dictionary<string, string>();
                    dc.Add("ID", CGDID.ToString());
                    int RecordCount = 0;
                    CGDModel entity = cgdB.GetCGD(dc,true,"ID",null,1,out RecordCount).FirstOrDefault();
                    hidGYSID.Value = entity.GYSID == null ? "" : entity.GYSID.ToString();
                    txtGYSJC.Text = entity.GYSJC;
                    if (entity.BMID != null && this.ddlDepartments.Items.FindByValue(entity.BMID.ToString()) != null)
                    {
                        this.ddlDepartments.SelectedValue = entity.BMID.ToString();
                        BindStaff(this.ddlDepartments.SelectedValue, this.ddlRSR, "请选择");
                        if (entity.RYID != null && this.ddlRSR.Items.FindByValue(entity.RYID.ToString()) != null)
                        {
                            this.ddlRSR.SelectedValue = entity.RYID.ToString();
                            hidStaff.Value = entity.RYID.ToString();
                        }
                    }                    
                }
                using (CGDMXBBLL cBLL = new CGDMXBBLL())
                {
                    List<CGDMXBModel> cgdmxM = cBLL.GetCGDMXB(CGDID);
                    repMXList.DataSource = cgdmxM;
                    repMXList.DataBind();
                    foreach (var mx in cgdmxM)
                    {
                        hidWLID.Value += "," + mx.WPID.ToString();
                        hidDWBH.Value += "," + mx.DWBH;
                        hidCounts.Value += "," + mx.SL.ToString();
                        hidPrices.Value += "," + mx.DJ.ToString();
                    }
                    hidWLID.Value = hidWLID.Value.Trim(',');
                    hidDWBH.Value = hidDWBH.Value.Trim(',');
                    hidCounts.Value = hidCounts.Value.Trim(',');
                    hidPrices.Value = hidPrices.Value.Trim(',');
                }
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            ZL_YWLSH ywItem = new ZL_YWLSH();
            bool isAdd = true;
            bool result;
            using (CGDBLL cgdB = new CGDBLL())
            {
                CGD entity = new CGD();
                if (CGDID != Guid.Empty)
                    entity = cgdB.GetModel(t => t.ID == CGDID);
                else
                {
                    entity.ID = Guid.NewGuid();
                    entity.ZT = 0;
                    entity.BH = GenYWLSH.GenBH(EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.CGD), out ywItem, out isAdd);
                }
                entity.BMID = Guid.Parse(this.ddlDepartments.SelectedValue);
                entity.RYID = Guid.Parse(hidStaff.Value);
                entity.CZRYID = this.currentUser.UserID;
                entity.GYSID = Guid.Parse(hidGYSID.Value);
                entity.CJSJ = DateTime.Now;
                entity.RQ = DateTime.Parse(this.txtRQ.Text.Trim());
                entity.DHSJ = DateTime.Parse(this.txtDHSJ.Text.Trim());
                List<CGDMXB> delMXB = entity.CGDMXB.ToList();
                string[] ids = hidWLID.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] dwbhs = hidDWBH.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] counts = hidCounts.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] prices = hidPrices.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < ids.Count(); i++)
                {
                    CGDMXB item = new CGDMXB();
                    item.ID = Guid.NewGuid();
                    item.WPID = Guid.Parse(ids[i]);
                    item.WPLX = EnumHelper.GetDescriptionByName(typeof(EnumKC_LX), EnumKC_LX.WL);
                    item.SL = Convert.ToDouble(counts[i]);
                    item.DJ = Convert.ToDouble(prices[i]);
                    item.DWBH = dwbhs[i];
                    item.CGDID = CGDID;
                    entity.CGDMXB.Add(item);
                }
                if (CGDID != Guid.Empty)
                    result = cgdB.Update<CGDMXB>(entity, delMXB);
                else result = cgdB.Insert(entity);
            }
            if (result)
            {
                btnSave.Visible = false;
                labResult.Text = "保存成功";
                using (ZL_YWLSHBLL zBLL = new ZL_YWLSHBLL())
                {
                    if (ywItem.ID != Guid.Empty)
                    {
                        if (isAdd)
                            zBLL.Insert(ywItem);
                        else
                            zBLL.Update(ywItem);
                    }
                }
            }
            else
                labResult.Text = "保存失败";
            BindData();
        }
    }
}